﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LaunchPad.Core.Controls
{
    /// <summary>
    /// Interaction logic for MapItemControl.xaml
    /// </summary>
    public partial class MapItemControl : UserControl
    {
        public static readonly DependencyProperty PositionXProperty = DependencyProperty.Register(
            "PositionX", typeof(double), typeof(MapItemControl), new PropertyMetadata(OnPositionXChanged));

        public static readonly DependencyProperty PositionYProperty = DependencyProperty.Register(
            "PositionY", typeof(double), typeof(MapItemControl), new PropertyMetadata(OnPositionYChanged));

        public MapItemControl()
        {
            InitializeComponent();

            MouseDown += new MouseButtonEventHandler(MapItemControl_MouseDown);
            MouseMove += new MouseEventHandler(MapItemControl_MouseMove);
            MouseUp += new MouseButtonEventHandler(MapItemControl_MouseUp);
        }

        public event EventHandler<MouseButtonEventArgs> Selected;
        public event EventHandler PositionChanging;
        public event EventHandler<PositionChangedEventArgs> PositionChanged;

        private Point _clickPos;

        public double PositionX
        {
            get { return (double)GetValue(PositionXProperty); }
            set { SetValue(PositionXProperty, value); }
        }

        public double PositionY
        {
            get { return (double)GetValue(PositionYProperty); }
            set { SetValue(PositionYProperty, value); }
        }

        public Rect Rect
        {
            get
            {
                double x = (double)GetValue(Canvas.LeftProperty);
                if (double.IsNaN(x))
                    x = 0;

                double y = (double)GetValue(Canvas.TopProperty);
                if (double.IsNaN(y))
                    y = 0;

                return new Rect(x, y, ActualWidth, ActualHeight);
            }
        }

        private static void OnPositionXChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            MapItemControl control = sender as MapItemControl;
            double x = (double)e.NewValue;
            control.SetValue(Canvas.LeftProperty, x - control.ActualWidth / 2);
        }

        private static void OnPositionYChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            MapItemControl control = sender as MapItemControl;
            double y = (double)e.NewValue;
            control.SetValue(Canvas.TopProperty, y - control.ActualHeight / 2);
        }

        void MapItemControl_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (IsMouseCaptured)
                ReleaseMouseCapture();
            Opacity = 1;

            Vector dist = e.GetPosition(Parent as Canvas) - _clickPos;

            e.Handled = true;
        }

        void MapItemControl_MouseMove(object sender, MouseEventArgs e)
        {
            if (IsMouseCaptured)
            {
                Vector dist = e.GetPosition(Parent as Canvas) - _clickPos;

                if (PositionChanged != null)
                    PositionChanged(this, new PositionChangedEventArgs(dist));

                e.Handled = true;
            }
        }

        void MapItemControl_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                _clickPos = e.GetPosition(Parent as Canvas);
                
                bool extendedSelect = Keyboard.Modifiers.HasFlag(ModifierKeys.Control);

                if (!(DataContext as IMappable).IsSelected 
                    || e.ClickCount == 2
                    || extendedSelect)
                {
                    if (Selected != null)
                        Selected(this, e);
                }

                if (PositionChanging != null)
                    PositionChanging(this, EventArgs.Empty);

                Opacity = 0.5;

                CaptureMouse();

                e.Handled = true;
            }
        }

        public void UpdateBindings()
        {
            BindingExpression topExpression = GetBindingExpression(MapItemControl.PositionXProperty);
            if (topExpression != null)
                topExpression.UpdateTarget();

            BindingExpression leftExpression = GetBindingExpression(MapItemControl.PositionYProperty);
            if (leftExpression != null)
                leftExpression.UpdateTarget();
        }

        public class PositionChangedEventArgs : EventArgs
        {
            public PositionChangedEventArgs(Vector distance)
            {
                Distance = distance;
            }

            public Vector Distance { get; private set; }
        }
    }
}
